package tmhprediction.optimization;

import java.io.File;
import java.util.HashMap;

import tmhprediction.classification.TMHClassifier;
import tmhprediction.eval.SolTmhEvaluator;
import tmhprediction.eval.TMHHelixEvaluator;
import tmhprediction.eval.TMHResidueEvaluator;
import tmhprediction.eval.WriteOutput;
import tmhprediction.main.TMHResultMap;

public class GridSearchRunnerSingleSetSoluble {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception
	{
		double c = Double.valueOf(args[0]);
		double g = Double.valueOf(args[1]);
		String outFolder = args[2];
		String train = args[3];
		String test = args[4];
		String index = args[5];
		double cuto = Double.valueOf(args[6]);
		
		c = Math.pow(2.0, c);
		g = Math.pow(2.0, g); 
		
		
		File o = new File(outFolder);
		o.mkdir();
		
		doIt(train, test, null, outFolder,index, c, g, cuto);

	}
	
	static void doIt(String trainingSet, String testSet, String[] options, String outFolder, String index, double c, double g, double cut) throws Exception
	{
		TMHClassifier classifier = new TMHClassifier(trainingSet, testSet, g,c,2000 );
		System.out.println("Running with C:"+classifier.getcost() +" and gamma:"+classifier.getGamma() + ". Train and test are: "+trainingSet+" "+testSet);
		System.out.println("Training done, now classification and evaluation");
		TMHResultMap resM = classifier.createResultMap();
		
		System.out.println("Calculate per residue scores");
		TMHResidueEvaluator.calcPerResidueScores(resM, outFolder+"/"+index+"_scoresPerRes");
		
		System.out.println("Calculating distinction to soluble scores");
		TMHResultMap result0 = TMHResidueEvaluator.evaluateProteins(resM);
		SolTmhEvaluator.printStatistics(result0);
		
		int[][] confm = SolTmhEvaluator.confusionSVM1(result0, cut);
		double tp = confm[0][0];
		double fp = confm[1][0];
		double fn = confm[0][1];
		double tn = confm[1][1];
		double sens = Double.valueOf(tp) / (tp + fn);
		double spec = Double.valueOf(tn) / (tn + fp);
		System.out.println("Sens is: "+sens);
		System.out.println("Spec is: "+spec);
//		
		System.out.println("TP: " + tp + " FP:" + fp + " FN:" + fn + " TN: " + tn);
		
		System.out.println("Writing out predictions in human readable format");
		WriteOutput.writeObservedAndPredicted(resM, outFolder+"/"+index+"_readablePrediction");
		
		System.out.println("Calculating and writing out per segment scores");
		TMHHelixEvaluator.berechneFormeln(TMHHelixEvaluator.finalHelixEvaluation(resM), outFolder+ "/"+index+"_scoresPerSeg");
		
		classifier.saveModel(outFolder+"/"+index+"_model.svm");
		
	}

}
